<template>
	<view class="root">

		<view class="big_result">
			
			<view class="newest_value">
				<view class="newest_guzhi_title">
					最新净值
				</view>
				<view class="newest_guzhi">
					{{newest_guzhi}}
				</view>
				<view class="precent">
					<view class="newest_precent_title">
						最新涨跌	
					</view>
					<view class="newest_precent"
					:class="newest_percent >= 0 ? 'red' : 'green' ">
						{{newest_percent}}%
					</view>
				</view>		
			</view>
			
			<!-- #ifndef MP-ALIPAY -->
			<picker @change="TabChange" :value="modelIndex" 
			:range="list" range-key="name"
			class="picker_select">
				<view class="picker-inner">
					<view class="picker-value">
						{{modelName}}模式
					</view>
				</view>
			</picker>
			<!-- #endif -->
			
			<view class="newest_ideas">
				<view class="freeNumber_ideas">
					<view class="freeNumber_sum_title">
						可赎份额
					</view>
					<view class="freeNumber_sum">
						{{freeNumberSum.toFixed(2)}}
					</view>
				</view>
				
				<view class="profit_ideas">
					<view class="profit_sum_title">
						累计盈亏
					</view>
					<view class="profit_sum"
					:class="profitSum >= 0 ? 'red' : 'green' ">
						{{profitSum.toFixed(2)}}({{(profitSum/sumMoney*100).toFixed(2)}}%)
					</view>
				</view>
				
				<view class="handle_ideas">
					<view class="handle_title">
						今日操作
					</view>
					<view class="handle_idea">
						{{ideasNumber.toFixed(2)}}
					</view>
				</view>
				
			</view>
		</view>
		
		<view class="result_list">
			
			<view class="result_nav">
				<view class="nav_day">
					日期
				</view>
				<view class="nav_money">
					金额
				</view>
				<view class="nav_number">
					拥有
				</view>
				<view class="nav_profit">
					盈亏
				</view>
				<view class="nav_idea">
					推荐
				</view>
			</view>
			
			
		<view class="result_swipe" 
		v-for="(jijin, index) in jijinArray"
		@click="toOrderDetail(jijin.id)"
		:key= index>
			<swipe-action
			class="root" :options="options" 
			:show="show" :index = index
			@button="sell"
			:disabled = "jijin.isIdea === 1 ? false : false ">
				<view class="result">
					<view class="result_day">
						{{jijin.buyDay.split('T')[0]}}
					</view>
					<view class="result_money">
						{{jijin.buyMoney}}
					</view>
					<view class="result_number">
						{{jijin.buyNumber}}
					</view>
					<view class="result_profit">
						{{(jijin.buyNumber*(_self.newest_guzhi-jijin.buyPrice)).toFixed(2)}}
						<!-- {{jijin.buyNumber}} -->
					</view>
					<view class="result_idea"
					:class="jijin.isIdea === 1 ? 'result_idea_active' : '' ">
						{{jijin.isIdea === 1 ? '卖出' : '无' }} 
					</view>
				</view>
			</swipe-action>
		</view>
			
		</view>
		

		
	</view>
</template>
<script>
	
	import swipeAction from '@/components/zhouWei-swipeAction';
	
	var _self;
	
	export default {
		data() {
			return {
				code: '', // 当前基金代码
				name: '', // 当前基金名称
				freeNumberSum: 0, //可赎金额合计
				sumMoney: 0, // 总金额
				newest_guzhi: 0, // 最新估值
				newest_percent: 0, // 最新涨跌
				profitSum: 0, // 盈亏总和
				ideasNumber: 0 , // 今日操作总和 
				model: 'zs', // 模式 zy zs
				modelName: '止损', // 模式 止盈 止损
				modelIndex: 1, // 模式下标
				jijinArray: [],
				sellId: '', //即将卖出基金的ID
				sellIndex: '', //即将卖出基金的Index
				show: false,
				list: [
					{
						name: '止盈',
						id: 'zy'
					},
					{
						name: '止损',
						id: 'zs'
					}
				],
				options: [
					{
						text: '卖出',
						style: {
							backgroundColor: '#dd524d'
						}
					},
					// {
					// 	text: '取消',
					// 	style: {
					// 		backgroundColor: '#007aff'
					// 	}
					// }
				],
			}
		},
		// 点击导航栏按钮触发
		onNavigationBarButtonTap() {
			uni.navigateTo({
				url:"../form/form?name="+this.name+"&code="+this.code
			})
		},
		// 下拉刷新触发
		onPullDownRefresh() {
			this.Init()
			// 停止当前页面下拉刷新
			uni.stopPullDownRefresh()
		},
		onLoad(data) {
			// 接收传递过来的数据
			this.code = data.code;
			this.name = data.name;
			_self = this;
			if(data.name != '' && data.name != undefined){
				uni.setNavigationBarTitle({
					title: data.name
				})
			}else {
				uni.setNavigationBarTitle({
					title: '基金详情'
				})
			}
			this.Init();
			
		},
		methods: {
			async Init() {
				// 初始化数据
				// this.code = '', // 当前基金代码
				// this.freeNumberSum = 0, //可赎金额合计
				// this.newest_guzhi = 0, // 最新估值
				// this.newest_percent = 0, // 最新涨跌
				// this.freeNumberSum = 0, // 可赎金额合
				this.profitSum = 0, // 盈亏总和
				this.ideasNumber = 0 , // 今日操作总和 
				this.sumMoney = 0, // 总金额
				// this.model = 'zs', // 模式 zy zs
				// this.jijinArray = [],
				
				uni.showLoading({
					title:'加载中...',
					mask:false
				})
				
				// 获取最新净值
				var result = await _self.store.getNewestNumber(_self.code)
				_self.newest_guzhi = result.guzhi
				_self.newest_percent = result.percent
				_self.freeNumberSum = result.freeNumberSum
				
				
				// 根据模式获取数据
				_self.jijinArray = await _self.store.findByCodeAndModel(this.code,this.model)
				_self.jijinArray.forEach((jijin) => {
					_self.profitSum += 
					jijin.buyNumber*(_self.newest_guzhi-jijin.buyPrice)
					_self.sumMoney += parseInt(jijin.buyMoney) 
					if(jijin.isIdea === 1){
						// 卖出
						_self.ideasNumber -= jijin.buyNumber
					}
				})
				
				uni.hideLoading()
			},
			
			// 卖出基金
			sell(content) {
				this.sellIndex = content.index 
				var jijin = this.jijinArray[this.sellIndex] // 获取卖出对象
				this.sellId =  jijin.id
				
				uni.showModal({
				    title: '警告',
				    content: '你确定要全部卖出吗？',
				    success: async function (res) {
				        if (res.confirm) {
							uni.showLoading({
								title:'卖出中...',
								mask:false
							})
							var sellresult = await _self.store.sell(_self.sellId)
							if(sellresult === 'ok'){
								// 清除页面列表数据
								_self.jijinArray.splice(_self.sellIndex, 1)
								_self.sellIndex = ''
								_self.sellId = ''
								uni.hideLoading()
								uni.showModal({
									content: "卖出成功！",
									showCancel: false
								});
								_self.Init()
							}
				        } else if (res.cancel) {
				            // console.log('用户点击取消');
				        }
				    }
				});
			},
			
			// 模式切换
			TabChange: function(e) {
				// console.log(e)
				this.model = this.list[e.detail.value].id
				this.modelName = this.list[e.detail.value].name
				this.modelIndex = e.detail.value
				this.Init()
			},
			
			// 查看订单详情
			toOrderDetail (id) {
				uni.navigateTo({
					url:'../../orderDetail/orderDetail?orderId='+id,
					animationType: 'pop-in',
					animationDuration: 200
				})
			}
			
			
		},
		components:{
			swipeAction,
		}
	}
</script>
<style>
	@import url("detail.css");
</style>
